package com.hw.paltfrom.service.weight;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.paltfrom.mapper.HwBaseAreaMapper;
import com.hw.paltfrom.mapper.HwDataEnterpriseHistoryMapper;
import com.hw.paltfrom.pojo.HwBaseArea;
import com.hw.paltfrom.pojo.HwDataEnterpriseHistory;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.MongodbUtil;
import com.hw.paltfrom.utils.RandomNumberUtil;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.utils.SpringUtil;
import com.hw.paltfrom.vo.DataEnterpriseHistoryVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

/**
 *01 称重检测（源头企业数据监管）
 * 崔孟超
 * 2019年11月6日15:34:31
 */
@Service
public class DataEnterpriseHistoryService {
	@Autowired
	private HwDataEnterpriseHistoryMapper hwDataEnterpriseHistoryMapper;
	@Autowired
	private HwBaseAreaMapper hwBaseAreaMapper;
	

	/**
	 * 源头企业遍历+分页+模糊查询 +刷新功能
	 * @param pojo
	 * @return
	 */
	public Map<String, Object> query(HwDataEnterpriseHistory pojo, DataEnterpriseHistoryVo vo) {
		// 源头企业名称
		String fEnterprisename = pojo.getfEnterprisename();
		// 车牌照
		String cpz = pojo.getCpz();
     	// 行政区域编号 ，市用户 不传，市用户查看县 传县地区编码，县用户 传该登陆账户的 地区编码
		String fAreacode = pojo.getfAreacode();
		String enterprisecode = pojo.getfEnterprisecode();
		Integer pageName = vo.getPageNum();
		Integer pageSize = vo.getPageSize();
		PageHelper.startPage(pageName, pageSize);
		Wrapper<HwDataEnterpriseHistory> namewrapper = new EntityWrapper<HwDataEnterpriseHistory>();

		// 查全市 不传fAreacode参数
		if (StringUtils.isNotBlank(fAreacode)) {
			namewrapper.eq("F_AreaCode", fAreacode);
		}
		if (StringUtils.isNotBlank(enterprisecode)) {
			namewrapper.eq("F_EnterpriseCode", enterprisecode);
		}
		if (StringUtils.isNotBlank(fEnterprisename)) {
			namewrapper.and().like("F_EnterpriseName", fEnterprisename);
		}

		if (StringUtils.isNotBlank(cpz)) {
			namewrapper.and().like("CPZ", cpz);
		}
		namewrapper.orderBy("JCSJ",false);
		// 全查出
		// if (!StringUtils.isNotBlank(fEnterprisename) &&
		// !StringUtils.isNotBlank(cpz)){
		// if (StringUtils.isBlank(fEnterprisename) && StringUtils.isBlank(cpz))
		// {
		//// namewrapper.like("F_AreaCode", fAreacode).orderBy("CCSJ");
		// namewrapper.like("F_AreaCode", fAreacode).orderBy("CCSJ");
		// }
		// if (StringUtils.isNotBlank(fEnterprisename)) {
		// namewrapper.eq("F_AreaCode",
		// fAreacode).and().like("F_EnterpriseName",
		// fEnterprisename).orderBy("JCSJ");
		// } else if (StringUtils.isNotBlank(cpz)) {
		// namewrapper.eq("F_AreaCode", fAreacode).and().like("CPZ",
		// cpz).orderBy("CCSJ");
		// } else if (StringUtils.isNotBlank(cpz) &&
		// StringUtils.isNotBlank(fEnterprisename)) {
		// namewrapper.eq("F_AreaCode",
		// fAreacode).and().like("F_EnterpriseName", fEnterprisename).or()
		// .like("CPZ", cpz).orderBy("CCSJ");
		// }
	
		List<HwDataEnterpriseHistory> hwDataEnterpriseHistory = hwDataEnterpriseHistoryMapper.selectList(namewrapper);
		PageInfo<HwDataEnterpriseHistory> info = new PageInfo<>(hwDataEnterpriseHistory);
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, info);
	}

	/**
	 * 详情+点击单个源头企业展示(暂不使用)
	 * @param pojo
	 * @return
	 */
	public Map<String, Object> particulars(HwDataEnterpriseHistory pojo) {
		// 应该是源头企业的id，非空校验应该在controller
		// 主键(GUID)
		String fId = pojo.getfId();
		// 行政区域编号
		String fAreacode = pojo.getfAreacode();
		if (!StringUtils.isNotBlank(fId) && !StringUtils.isNotBlank(fAreacode)) {
			return ResponeResultUtil.fail(HWSystemCode.FAIL);
		}
		HwDataEnterpriseHistory hwDataEnterpriseHistory = hwDataEnterpriseHistoryMapper.selectById(fId);
		if (hwDataEnterpriseHistory == null) {
			return ResponeResultUtil.fail(HWSystemCode.FAIL);
		}
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, hwDataEnterpriseHistory);
	}

	/**
	 * 源头企业数据监管的添加
	 * @param pojo
	 * @return
	 */
	public Map<String, Object> insert(HwDataEnterpriseHistory pojo) {
		pojo.setfId(RandomNumberUtil.getNumber());
		// pojo.setJcsj(new Timestamp(System.currentTimeMillis()));
		Integer insert = hwDataEnterpriseHistoryMapper.insert(pojo);
		if (insert != 1) {
			return ResponeResultUtil.fail(HWSystemCode.FAIL);
		} else {
			MongodbUtil mongo = SpringUtil.getBean(MongodbUtil.class);
			mongo.insert(pojo);
			return ResponeResultUtil.success(HWSystemCode.SUCCESS);
		}
	}

	/**
	 * 左侧树型选择的节点遍历（暂不需要）
	 * 
	 * @param pojo
	 * @return
	 */
	public Map<String, Object> List(HwDataEnterpriseHistory pojo, DataEnterpriseHistoryVo vo) {
		// 行政区域编号
		String fAreacode = pojo.getfAreacode();
		Integer pageName = vo.getPageNum();
		Integer pageSize = vo.getPageSize();
		Wrapper<HwBaseArea> namewrapper = new EntityWrapper<HwBaseArea>();
		namewrapper.eq("F_AreaCode", fAreacode).eq("F_Layer", 0);
		List<HwBaseArea> DataEnterpriseHistor = hwBaseAreaMapper.selectList(namewrapper);
		Wrapper<HwDataEnterpriseHistory> namewrapper1 = new EntityWrapper<HwDataEnterpriseHistory>();
		if (!DataEnterpriseHistor.isEmpty()) {
			namewrapper.orderBy("CCSJ");
			PageHelper.startPage(pageName, pageSize);
			List<HwDataEnterpriseHistory> DataEnterprise = hwDataEnterpriseHistoryMapper.selectList(namewrapper1);
			if (DataEnterprise.isEmpty()) {
				return ResponeResultUtil.fail(HWSystemCode.SUCCESS);
			} else {
				PageInfo<HwDataEnterpriseHistory> info = new PageInfo<>(DataEnterprise);
				return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, info);
			}
		} else {
			PageHelper.startPage(pageName, pageSize);
			List<HwDataEnterpriseHistory> DataEnterprise = hwDataEnterpriseHistoryMapper.selectList(namewrapper1);
			if (DataEnterprise.isEmpty()) {
				return ResponeResultUtil.fail(HWSystemCode.SUCCESS);
			} else {
				namewrapper.eq("F_AreaCode", fAreacode).orderBy("CCSJ");
				PageInfo<HwDataEnterpriseHistory> info = new PageInfo<>(DataEnterprise);
				return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, info);
			}
		}
	}
}
